package negModel;

import negModelExcep.eParteNoFunciona;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Tanque extends PartePerformance {

	private double capacidad;
	private Combustible combustible;
	
	public Tanque(){}
	
	public Tanque (int vidaUtil, double peso,String detalle,AlgoPesos precio,boolean removible,int performance, double capacidad, Combustible comb){
		this.setMaxVU(vidaUtil);
		this.arreglar();
		this.setPeso(peso);
		
		this.setDetalle(detalle);
		this.setPrecio(precio);
		this.setPerformance(performance);
		this.setRemovible(removible);
		this.capacidad=capacidad;
		this.combustible = comb;
		this.setAportevmax(combustible.getOctanos());
		
	}
	
	public Tanque (Element xmlElement) {
		this.setMaxVU(Integer.parseInt(xmlElement.getAttribute("maxVU")));
		this.setVidaUtil(Integer.parseInt(xmlElement.getAttribute("vidaUtil")));
		this.setPeso(Double.parseDouble(xmlElement.getAttribute("peso")));
		this.setAportevmax(Float.parseFloat(xmlElement.getAttribute("aportevmax")));
		this.setDetalle(xmlElement.getAttribute("detalle"));
		this.setPrecio(new AlgoPesos(Double.parseDouble(xmlElement.getAttribute("precio"))));
		this.setRemovible(Boolean.parseBoolean(xmlElement.getAttribute("removible")));
		this.setPerformance(Integer.parseInt(xmlElement.getAttribute("performance")));
		
		this.capacidad=Double.parseDouble (xmlElement.getAttribute("capacidad"));
		NodeList  nodosComb=xmlElement.getElementsByTagName("combustible");
			
		for(int nc=0;nc<nodosComb.getLength();nc++){
			this.combustible= new Combustible((Element)nodosComb.item(nc));
		}
						
	}
	
	public Element toXml(Document doc) {
		Element xmlElement = doc.createElement("parte");
		xmlElement.setAttribute("Clase","tanque");
		
		xmlElement.setAttribute("maxVU", ""+ this.getMaxVU());
		xmlElement.setAttribute("vidaUtil", ""+this.getVidaUtil());
		xmlElement.setAttribute("peso", ""+this.getPeso());
		xmlElement.setAttribute("aportevmax", ""+ this.aportevmax());
		xmlElement.setAttribute("detalle", this.getDetalle());
		xmlElement.setAttribute("precio", ""+(this.getPrecio()).getMonto());
		xmlElement.setAttribute("removible", ""+this.esRemovible());
		xmlElement.setAttribute("performance", ""+this.getPerformance());
		
		xmlElement.setAttribute("capacidad", ""+this.capacidad);
		xmlElement.appendChild(this.combustible.toXml(doc));
				
		return xmlElement;
	}
	
	public double funcionar() throws eParteNoFunciona {
		if (this.combustible.getCantidad()<= 0)
			throw new eParteNoFunciona(this);
		else{
			this.gastar();
			this.combustible.gastar();
		}
		return this.combustible.getOctanos();
	}
	
	public double getCapacidad (){
		return this.capacidad;
	}
	
	public double getCantComb(){
		Double d = new Double(this.combustible.getCantidad());
		return d.longValue();
	}
	
	public void cargarComb(){
		this.combustible.setCantidad(this.capacidad);
	}
	
	public double getPeso(){
		return super.getPeso()+this.combustible.pesar();
	}
	
}
